{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "import sys\n",
    "import pymysql\n",
    "\n",
    "sys.path.append(\"/home/jie/.key\")\n",
    "# 本地密码存储文件\n",
    "from sql_key import password\n",
    "\n",
    "connection = pymysql.connect(\n",
    "    host=\"localhost\",  # MySQL数据库的主机\n",
    "    user=\"root\",  # MySQL用户名\n",
    "    password=password,  # MySQL密码\n",
    "    database=\"industry\",  # 你要插入数据的数据库\n",
    "    charset=\"utf8mb4\",\n",
    "    cursorclass=pymysql.cursors.DictCursor,\n",
    ")\n",
    "\n",
    "def get_patent_cnt_by_com_year(company_name, year):\n",
    "    \"\"\"\n",
    "    根据公司名和年份获取专利数量\n",
    "    \"\"\"\n",
    "    sql = \"\"\"\n",
    "    select applicant as company_name, YEAR(application_date) as year, count(*) as cnt from patent_persons \n",
    "    where applicant=\"{company_name}\" and YEAR(application_date)={year};\n",
    "    \"\"\".strip()\n",
    "    sql = sql.format(company_name=company_name, year=year)\n",
    "\n",
    "    with connection.cursor() as cursor:\n",
    "        res = cursor.execute(\n",
    "            sql,\n",
    "        )\n",
    "        # 提交事务\n",
    "        # connection.commit()\n",
    "        res = cursor.fetchone()\n",
    "\n",
    "    return res\n",
    "\n",
    "\n",
    "# sql = \"\"\"\n",
    "# SELECT YEAR(grant_publication_date) AS year, COUNT(DISTINCT applicant) AS applicant_count FROM search\n",
    "# GROUP BY YEAR(grant_publication_date)\n",
    "# ORDER BY year;\n",
    "# \"\"\".strip()\n",
    "\n",
    "\n",
    "# res = run_sql(sql=sql)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# select department, salary, count(salary) from employees group by salary;"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "res = get_patent_cnt_by_com_year(\"上海丙嘉精密机械科技有限公司\", 2016)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'company_name': '上海丙嘉精密机械科技有限公司', 'year': 2016, 'cnt': 20}"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "llm",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
